package cn.com.csg.smartenergy.modules.biz.energy.controller;

import cn.com.csg.smartenergy.modules.biz.energy.service.EnergyIotDeviceService;
import cn.com.csg.smartenergy.modules.biz.entity.IotDeviceEntity;
import cn.com.csg.smartenergy.modules.biz.task.entity.TaskEntity;
import cn.com.csg.smartenergy.modules.biz.task.service.TaskService;
import cn.com.csg.smartenergy.util.OperResult;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/energy/iot")
public class EnergyIotController {
    @Autowired
    private EnergyIotDeviceService deviceService;
    @Autowired
    private TaskService taskService;
    @Autowired
    public RedisTemplate<String,String> redisTemplate;

    /**
     * 列表
     */
    @ResponseBody
    @GetMapping("/list")
    public OperResult list(@RequestParam(required = false) IotDeviceEntity params){
        deviceService.getBaseMapper().selectList(null).forEach(System.out::println);
        return OperResult.ok().put("data", null);
    }
    /**
     * 列表==>定时任务status
     */
    @ResponseBody
    @GetMapping("/list2")
    public OperResult list2(@RequestParam(required = false) IotDeviceEntity params){
        List list = deviceService.getAllDeviceInfoAndJobStatus(params);
        return OperResult.ok().put("data", list);
    }

    /**
     * 得到产品list
     */
    @ResponseBody
    @RequestMapping("/getIotProductService/{id}")
    public OperResult getIotProductService(@PathVariable("id") Long id){
        return deviceService.getIotProductService(id);
    }

    /**
     * 得到产品属性对应list
     */
    @ResponseBody
    @RequestMapping("/getServicePropList/{id}")
    public OperResult getServicePropList(@PathVariable("id") Long id){
        return deviceService.getServicePropList(id);
    }

    /**
     * 上下线
     */
    @ResponseBody
    @PostMapping("/online")
    public OperResult online(@RequestBody(required = false) IotDeviceEntity params){
        deviceService.online(params);
        return OperResult.ok();
    }
    /**
     * 启动or移除定时任务（心跳定时任务）
     * @param taskEntity
     * @return
     */
    @SneakyThrows
    @ResponseBody
    @PostMapping("/task/taskStartOrRemove")
    public OperResult taskStartOrRemove(@RequestBody TaskEntity taskEntity){
        try {
            taskEntity.setDs("energy");
            taskService.taskStartOrRemove(taskEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return OperResult.error(e.getMessage());
        }
        return OperResult.ok();
    }
    /**
     * 启动or移除定时任务（发送模拟数据）
     * @param taskEntity
     * @return
     */
    @SneakyThrows
    @ResponseBody
    @PostMapping("/task/sendMessages")
    public OperResult sendMessages(@RequestBody TaskEntity taskEntity){
        try {
            taskEntity.setDs("energy");
            taskService.sendMessages(taskEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return OperResult.error(e.getMessage());
        }
        return OperResult.ok();
    }
}

